package com.uenpay.im.adapter;

import android.content.Context;
import android.text.TextUtils;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.qiyunxin.android.http.adapter.QiYunXinAdapter;
import com.qyx.android.database.FriendDB;
import com.qyx.android.database.GroupTalkDbManager;
import com.qyx.android.entity.FriendEntity;
import com.qyx.android.entity.GroupTalkEntity;
import com.qyx.android.message.type.OperationType;
import com.qyx.android.message.type.PacketType;
import com.qyx.android.protocol.QyxTopMsg;
import com.qyx.android.weight.edittext.Emoji;
import com.uenpay.im.IMApplication;
import com.uenpay.im.R;
import com.uenpay.im.ui.old.activity.chat.manage.ChatItemDataManager;
import com.uenpay.im.configuration.APIConfiguration;
import com.uenpay.im.service.MsgContentType2Name;
import com.uenpay.im.utilities.QyxDateUtils;
import com.uenpay.im.weight.RoundImageView;

import java.text.MessageFormat;
import java.util.ArrayList;
import java.util.Date;

public class TopListMsgAdapter extends QiYunXinAdapter {

	private static final String TAG = "TopListMsgAdapter";

	private LayoutInflater mInflater;
	ArrayList<QyxTopMsg> circleMsgs;
	private Context context;
	private Emoji mEmoji;
	private GroupTalkDbManager groupTalkDbManager = null;

	public TopListMsgAdapter(Context context, ArrayList<QyxTopMsg> circleMsgs) {
		this.mInflater = LayoutInflater.from(context);
		this.circleMsgs = circleMsgs;
		this.context = context;
		this.mEmoji = new Emoji();
		groupTalkDbManager = new GroupTalkDbManager();
	}

	@Override
	public int getCount() {
		return circleMsgs.size();
	}

	@Override
	public Object getItem(int arg0) {
		return circleMsgs.get(arg0);
	}

	@Override
	public long getItemId(int arg0) {
		return arg0;
	}

	public void setData(ArrayList<QyxTopMsg> data) {
		this.circleMsgs = data;
		notifyDataSetChanged();
		Log.d(TAG, "notifyDataSetChanged");
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		Log.d(TAG, "getView position " + position);
		ViewHolder holder;
		QyxTopMsg circleMsg = circleMsgs.get(position);

		String from_icon = "";
		String from_name = "";
		int new_msg_notice = 1;
		if (circleMsg.sessionModel.getSessionType() == PacketType.PERSONAL) {
			from_icon = APIConfiguration.getAvatarUrlNormal(circleMsg.sessionModel.getSessionId() + "", PacketType.PERSONAL);
			FriendEntity entity = FriendDB.getFriend(circleMsg.sessionModel.getSessionId() + "");
			if (entity != null) {
				if (entity.is_remind == 1) {
					new_msg_notice = 0;
				} else {
					new_msg_notice = 1;
				}
				if (!TextUtils.isEmpty(entity.nick_name)) {
					from_name = entity.nick_name;
				}
			} else {
				new_msg_notice = 1;
			}
			if (!TextUtils.isEmpty(IMApplication.config.getFriendsRemarkName(circleMsg.sessionModel.getSessionId() + ""))) {
				from_name = IMApplication.config.getFriendsRemarkName(circleMsg.sessionModel.getSessionId() + "");
			}
			if (TextUtils.isEmpty(from_name)) {
				from_name = circleMsg.chat_name;
			}
		} else if (circleMsg.sessionModel.getSessionType() == PacketType.GROUP) {
			from_icon = APIConfiguration.getAvatarUrlNormal(circleMsg.sessionModel.getSessionId() + "", PacketType.GROUP);
			from_name = circleMsg.chat_name;
			GroupTalkEntity groupTalk = groupTalkDbManager.queryGroupByGroupId(circleMsg.sessionModel.getSessionId());

			if (groupTalk != null && groupTalk.is_remind == 1) {
				new_msg_notice = 0;
			} else {
				new_msg_notice = 1;
			}
		}

		if (convertView == null) {
			holder = new ViewHolder();
			convertView = mInflater.inflate(R.layout.layout_chat_msg_list_item, parent, false);
			holder.img = (RoundImageView) convertView.findViewById(R.id.image);
			holder.time = (TextView) convertView.findViewById(R.id.time);
			holder.info = (TextView) convertView.findViewById(R.id.last_message);
			holder.msg_count = (TextView) convertView.findViewById(R.id.unread_count);
			holder.at_me_tv = (TextView) convertView.findViewById(R.id.at_me_tv);
			holder.new_msg_count = (TextView) convertView.findViewById(R.id.new_msg_count);
			holder.msg_item_username = (TextView) convertView.findViewById(R.id.title);
			holder.is_editor = (ImageView) convertView.findViewById(R.id.is_edited);
//			holder.new_msg_iv = (ImageView) convertView.findViewById(R.id.new_msg_iv);
			convertView.setTag(holder);
		} else {
			holder = (ViewHolder) convertView.getTag();
		}

		holder.img.SetUrl(from_icon);
		holder.is_editor.setVisibility(View.GONE);
		holder.msg_item_username.setText(from_name);
		if (circleMsg.msg_time != 0) {
			long msg_time = circleMsg.msg_time * 1000;
			holder.time.setText(QyxDateUtils.generateDateTimeString(new Date(msg_time)));
		}

		if (new_msg_notice == 0) {
			holder.msg_count.setVisibility(View.GONE);
			if (circleMsg.msg_count > 0) {
//				holder.new_msg_iv.setVisibility(View.VISIBLE);
				holder.new_msg_count.setVisibility(View.VISIBLE);
				holder.new_msg_count.setText(MessageFormat.format(context.getResources().getString(R.string.new_msg_count),
						circleMsg.msg_count));
			} else {
				holder.new_msg_count.setVisibility(View.GONE);
//				holder.new_msg_iv.setVisibility(View.GONE);
			}
		} else {
			holder.new_msg_count.setVisibility(View.GONE);
//			holder.new_msg_iv.setVisibility(View.GONE);
			// if (circleMsg.msg_count >= 100) {
			// holder.msg_count.setText("99+");
			// } else {
			holder.msg_count.setText(circleMsg.msg_count + "");
			// }
			if (circleMsg.msg_count > 0) {
				holder.msg_count.setVisibility(View.VISIBLE);
			} else {
				holder.msg_count.setVisibility(View.GONE);
			}
		}
		if (circleMsg.is_editor == 1) {
			holder.is_editor.setVisibility(View.VISIBLE);
		} else {
			holder.is_editor.setVisibility(View.GONE);
		}
		// TODO 设置公众号消息类型4
		if (circleMsg.sessionModel.getOperationType() == OperationType.PUBLIC_NUM) {
			holder.msg_item_username.setTextColor(context.getResources().getColor(R.color.the_color_blue_dark));
		} else {
			holder.msg_item_username.setTextColor(context.getResources().getColor(R.color.the_color_black));
		}

		if (circleMsg.is_at == 1) {
			holder.at_me_tv.setVisibility(View.VISIBLE);
		} else {
			holder.at_me_tv.setVisibility(View.GONE);
		}

		String tempContent = MsgContentType2Name.GetMsgContentTypeName(circleMsg.msg_content_type);
		if (!TextUtils.isEmpty(tempContent)) {
			circleMsg.content = tempContent;
		}
		if (circleMsg.sessionModel.getSessionType() == PacketType.GROUP
				&& circleMsg.from_cust_id != Long.valueOf(IMApplication.getCustId())) {
			String from_cust_name = IMApplication.config.getFriendsRemarkName(circleMsg.from_cust_id + "");
			if (TextUtils.isEmpty(from_cust_name)) {
				from_cust_name = circleMsg.from_cust_name;
			}
			if (TextUtils.isEmpty(from_cust_name)) {
				holder.info.setText(mEmoji.getSpannedRate(circleMsg.content, IMApplication.getAppContext(), 3));
			} else {
				holder.info.setText(mEmoji.getSpannedRate(from_cust_name + ":" + circleMsg.content, IMApplication.getAppContext(), 3));
			}
		} else {
			holder.info.setText(mEmoji.getSpannedRate(circleMsg.content, IMApplication.getAppContext(), 3));
		}
		if (!TextUtils.isEmpty(circleMsg.content_json) && circleMsg.content_json.indexOf("content_param") > -1) {
			ChatItemDataManager.setSystemTv(holder.info, circleMsg.content_json, circleMsg.content);
		}
		return convertView;
	}

	public final class ViewHolder {
		private RoundImageView img;
		private ImageView is_editor, new_msg_iv;
		private TextView info;
		private TextView time;
		private TextView msg_count, at_me_tv, new_msg_count;
		private TextView msg_item_username;
	}

}
